package com.kasinf.sample.websocket.mq;

import com.kasinf.framework.cache.utils.RedisUtils;
import com.kasinf.framework.websocket.core.sender.AbstractWebSocketMessageSender;
import com.kasinf.framework.websocket.core.session.WebSocketSessionManager;
import org.springframework.stereotype.Component;

/**
 * @Author : Kaiser
 * @Date: 2025/6/23  16:28
 */
@Component
public class RedisWebSocketMessageSender extends AbstractWebSocketMessageSender {
    public RedisWebSocketMessageSender(WebSocketSessionManager sessionManager) {
        super(sessionManager);
    }

    @Override
    public void send(Integer userType, Long userId, String messageType, String messageContent) {
        sendRedisMessage(null, userId, userType, messageType, messageContent);
    }

    @Override
    public void send(Integer userType, String messageType, String messageContent) {
        sendRedisMessage(null, null, userType, messageType, messageContent);
    }

    @Override
    public void send(String sessionId, String messageType, String messageContent) {
        sendRedisMessage(sessionId, null, null, messageType, messageContent);
    }

    /**
     * 通过 Redis 广播消息
     *
     * @param sessionId Session 编号
     * @param userId 用户编号
     * @param userType 用户类型
     * @param messageType 消息类型
     * @param messageContent 消息内容
     */
    private void sendRedisMessage(String sessionId, Long userId, Integer userType,
                                  String messageType, String messageContent) {
        RedisWebSocketMessage mqMessage = new RedisWebSocketMessage();

        mqMessage.setSessionId(sessionId);
        mqMessage.setUserId(userId);
        mqMessage.setUserType(userType);
        mqMessage.setMessageType(messageType);
        mqMessage.setMessageContent(messageContent);
        RedisUtils.publish("redis-websocket-message", mqMessage);
    }

}
